<template>
	<view>
		<scan-layout ref="scanLayout" :tabsList="tabsList" sum-confirm-text="提交入库" @add-click="addClick" @confirm="confirmClick" @product-change="productChange"></scan-layout>
	</view>
</template>

<script>
import ScanLayout from './components/scanLayout/scanLayout.vue';
import { mapMutations, mapActions } from 'vuex';
export default {
	components: {
		ScanLayout
	},
	onLoad(obj) {
		if (obj.single) {
			//单锁收货
			uni.setNavigationBarTitle({
				title: '单锁收货'
			});
		}
	},
	data() {
		return {
			tabsList: [
				{
					name: '入库扫描'
				},
				{
					name: '数据汇总'
				}
			],
			form: {}
		};
	},
	beforeDestroy() {
		this.resetScanDeviceInfo();
		this.resetSumList();
	},
	methods: {
		...mapActions({
			inStorageDeviceOther: 'storageOrder/inStorageDeviceOther', //创建
			getDeviceByCustomer: 'storageOrder/getDeviceByCustomer',
			deviceInStorage: 'storageOrder/deviceInStorage' //更新入库状态
		}),
		...mapMutations({
			resetSumList: 'storageOrder/resetSumList',
			dropSumListItem: 'storageOrder/dropSumListItem',
			setSumList: 'storageOrder/setSumList',
			resetScanDeviceInfo: 'storageOrder/resetScanDeviceInfo',
			updateCanAdd: 'storageOrder/updateCanAdd',
			updateScanDeviceInfo: 'storageOrder/updateScanDeviceInfo'
		}),
		addClick() {
			this.$refs.scanLayout.addToList();
		},
		productChange(e) {
			let customerNo = this.$store.state.user.userInfo.teamNo;
			if (e.isValid) {
				this.getDeviceByCustomer({
					code: this.$store.state.storageOrder.scanDeviceInfo.code
				})
					.then(res => {
						console.log(res);
						if (res && res.code == 0) {
							if (res.data === null) {
								//查不到，新建
								this.updateCanAdd(true);
								this.updateScanDeviceInfo({ isNewDevice: true });
							} else if (res.data.customer != customerNo) {
								//不属于当前经销商，新建
								this.updateCanAdd(true);
								this.updateScanDeviceInfo({ isNewDevice: true });
							} else if (res.data.ckZt == 1 || res.data.xsZt == 1) {
								this.updateCanAdd(false);
								uni.showToast({
									title: '机身码已出库',
									icon: 'none'
								});
							} else if (res.data.rkZt == 1) {
								//已在库中
								this.updateCanAdd(false);
								uni.showToast({
									title: '机身码已入库',
									icon: 'none'
								});
							} else {
								this.updateCanAdd(true);
							}
						} else {
							uni.showToast({
								title: '查询机身码失败',
								icon: 'none'
							});
							this.updateCanAdd(false);
						}
					})
					.catch(err => {
						console.log(err);
						this.updateCanAdd(false);
						uni.showToast({
							title: '网络错误，查询机身码失败',
							icon: 'none'
						});
					});
			} else {
				this.updateCanAdd(false);
			}
		},
		confirmClick() {
			this.$confirm('提示', '确定入库？', this.inStorage);
		},
		async inStorage() {
			uni.showLoading({
				mask: true,
				title: '请求中'
			});
			let list = this.$store.state.storageOrder.sumList;
			let successCodeArr = [];
			let failCodeArr = [];
			for (let i = 0; i < list.length; i++) {
				let data = list[i];
				if (data.checked) {
					try {
						let res = null;
						if (data.isNewDevice) {
							res = await this.inStorageDeviceOther({
								...data
							});
							if (res && res.code == 0) {
								res = await this.deviceInStorage({
									...data
								});
							}
						} else {
							res = await this.deviceInStorage({
								...data
							});
						}

						if (res && res.code == 0) {
							successCodeArr.push(data.code);
						} else {
							failCodeArr.push(data.code);
						}
					} catch (e) {
						failCodeArr.push(data.code);
						continue;
					}
				}
			}
			uni.hideLoading();
			if (failCodeArr.length > 0) {
				let newList = [];
				list.forEach(e => {
					if (successCodeArr.indexOf(e.code) == -1) {
						newList.push(e);
					}
				});
				this.setSumList(newList);
				uni.showModal({
					title: `有${failCodeArr.length}条数据请求失败，请重新尝试`,
					showCancel: false
				});
			} else {
				this.dropSumListItem();
				uni.showToast({
					icon: 'success',
					title: '提交成功'
				});
			}
		}
	}
};
</script>

<style lang="less">
@import './styles/table.less';
</style>
